package com.htfyun.roobofactory.factorytest;

import android.content.Context;
import android.media.AudioManager;
import android.media.MediaPlayer;
import android.os.Bundle;
import android.os.Handler;
import android.support.v4.app.ActivityCompat;
import android.util.Log;
import android.view.View;
import android.widget.Button;

import com.htfyun.roobofactory.BaseActivity;
import com.htfyun.roobofactory.R;

/**
 * Created by sst on 2018/7/8.
 */

public class SpeakerTestActivity extends BaseActivity {
    private Button btnPass;
    private Button btnFail;

    private MediaPlayer mediaPlayer;
    private AudioManager mAudioManager = null;

    private Handler mHandler = new Handler();

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        setContentView(R.layout.activity_factorytest_speaker);

        btnPass = (Button) findViewById(R.id.btnPass);
        btnFail = (Button) findViewById(R.id.btnFail);
        btnPass.setOnClickListener(onPassOrFailClickListener);
        btnFail.setOnClickListener(onPassOrFailClickListener);

        mAudioManager = (AudioManager)getApplicationContext().getSystemService(Context.AUDIO_SERVICE);
    }

    @Override
    protected void onResume() {
        super.onResume();
        initMediaPlayer();
        setBtnPassOrFailEnabled(false);
        mHandler.postDelayed(new Runnable() {
            @Override
            public void run() {
                setBtnPassOrFailEnabled(true);
            }
        }, 4000);
    }

    @Override
    protected void onPause() {
        super.onPause();

        deinitMediaPlayer();
    }

    private void initMediaPlayer() {

        if (mediaPlayer != null) {
            deinitMediaPlayer();
        }
        mediaPlayer = MediaPlayer.create(this, R.raw.music_test_channel);
        int currentVolume = mAudioManager.getStreamVolume(AudioManager.STREAM_MUSIC);
        int maxVolume = mAudioManager.getStreamMaxVolume(AudioManager.STREAM_MUSIC);
        Log.d(TAG,"currentVolume: " + currentVolume + " maxVolume: " + maxVolume);
        mAudioManager.setStreamVolume(AudioManager.STREAM_MUSIC,maxVolume,0);
        mediaPlayer.start();//开始音频的播放
    }

    private void deinitMediaPlayer() {
        if(mediaPlayer !=null) {
            mediaPlayer.stop();
            mediaPlayer.release();
            mediaPlayer = null;
        }
    }

    private void setBtnPassOrFailEnabled(boolean enabled) {
        btnFail.setEnabled(enabled);
        btnPass.setEnabled(enabled);
    }
    private View.OnClickListener onPassOrFailClickListener = new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            if (v == btnPass) {

                setResult(RESULT_OK);
                ActivityCompat.finishAfterTransition(getActivity());

            } else if (v == btnFail) {

                setResult(RESULT_CANCELED);
                ActivityCompat.finishAfterTransition(getActivity());

            }
        }
    };
}
